<?php
declare(strict_types=1);

namespace ServerApp\lib\server\tcp;

use ServerApp\lib\config\LogConfig;
use ServerApp\lib\memory\Chan;
use ServerApp\lib\process\SProcess;
use ServerApp\lib\server\model\swoole\DeviceList;
use ServerApp\lib\server\model\swoole\FdList;
use ServerApp\lib\util\ServerHelper;

/**
 * Class createTcp
 * @package ServerApp\lib\server\tcp
 */
class createTcp
{
    private static $instance = null;

    /**
     * createTcp constructor.
     * @param ITcp $func
     */
    private function __construct(ITcp $func)
    {
        $port = 9400;
//        Chan::getInstance(Tcp::LONG_TASK);
        ServerHelper::init(false, [
            "PORT=" . $port,
            "CONFIG_PATH=" . BASE_DIR . '/apps/tao/config/config.php',
            "FUNCTION_PATH=". BASE_DIR . '/third/WebApp/preload/Define.php',
            "SERVER_NAME=android",
        ]);
        LogConfig::init();
        LogConfig::$instance = null;
        DeviceList::getInstance();
        FdList::getInstance();

//        (new SProcess(function(){
//            AndroidPush::init();
//        }))->start();

        (new SProcess(function()use($func){
            (new Tcp($func))->run();
        }))->start();
    }

    /**
     * @param ITcp $func
     * @return null|createTcp
     */
    public static function getInstance(ITcp $func)
    {
        if (self::$instance === null) {
            self::$instance = new self($func);
        }
        return self::$instance;
    }
}